package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.InviteFriendsAdapter;
import com.sanpu.kxe.business.main.adapter.InviteFriendsPagerAdapter;
import com.sanpu.kxe.business.main.bean.InviteFriendsOutput;
import com.sanpu.kxe.business.main.contract.InviteFriendsContract;
import com.sanpu.kxe.business.main.presenter.InviteFriendsPresenter;
import com.sanpu.kxe.common.view.HorizontalListView;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Description：邀请好友
 * Created by 薛勇军 on 2019/4/15.
 */

public class InviteFriendsActivity extends BaseTitleActivity implements InviteFriendsContract.View {

    @Bind(R.id.viewpage)
    ViewPager vViewpage;
    @Bind(R.id.invitationHlv)
    HorizontalListView invitationHlv;

    private InviteFriendsAdapter mFriendsAdapter;

    private InviteFriendsPagerAdapter mPagerAdapter;

    private InviteFriendsContract.Presenter mPresenter;

    private List<InviteFriendsOutput> mInviteFriendsOutputs = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_invite_friends;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("邀请好友");
        commonTitle.showRightImageView(R.drawable.ic_share);

        mFriendsAdapter = new InviteFriendsAdapter(mContext, new ArrayList<InviteFriendsOutput>(), Integer.MAX_VALUE);
        invitationHlv.setAdapter(mFriendsAdapter);

        mPresenter = new InviteFriendsPresenter(this);
        mPresenter.inviteFriends();
    }

    @Override
    protected void setListener() {
        super.setListener();
        invitationHlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                vViewpage.setCurrentItem(position);
            }
        });
        vViewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position < mInviteFriendsOutputs.size()) {
                    for (int index = 0; index < mInviteFriendsOutputs.size(); index++) {
                        mInviteFriendsOutputs.get(index).setChoose(false);
                    }
                    mInviteFriendsOutputs.get(position).setChoose(true);
                    mFriendsAdapter.setNotifyData(mInviteFriendsOutputs, Integer.MAX_VALUE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onShareClick();
            }
        });
    }

    @Override
    public void showDataView(List<InviteFriendsOutput> inviteFriendsOutputs) {
        if (inviteFriendsOutputs != null && inviteFriendsOutputs.size() > 0) {
            mInviteFriendsOutputs.clear();
            mInviteFriendsOutputs.addAll(inviteFriendsOutputs);
            mInviteFriendsOutputs.get(0).setChoose(true);
            mFriendsAdapter.setNotifyData(mInviteFriendsOutputs, Integer.MAX_VALUE);
            mPagerAdapter = new InviteFriendsPagerAdapter(getSupportFragmentManager(), inviteFriendsOutputs);
            vViewpage.setAdapter(mPagerAdapter);
        }
    }

    /**
     * 分享点击事件
     */
    private void onShareClick() {
        FrameLayout shareViewFl = mPagerAdapter.getShareView(vViewpage.getCurrentItem());
        if (shareViewFl != null) {
            shareViewFl.setDrawingCacheEnabled(true);
            Bitmap mClipScreen = shareViewFl.getDrawingCache();
            if (mClipScreen == null) return;
            mPresenter.shareImageBitmap(mClipScreen);
        } else {
            ToastUtil.show("图片资源获取失败！");
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(InviteFriendsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
